<?php
/**
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: nimophp.php 2009-07-22 06:20:21Z andery $
 */
defined('NIMO_BEGIN_TIME') or define('NIMO_BEGIN_TIME', microtime()); //启动时间。
defined('NIMO_PATH') or define('NIMO_PATH', dirname(__FILE__)); //框架路径
defined('NIMO_DEBUG') or define('NIMO_DEBUG', true); //DUBUG开关
defined('NIMO_VENDORS') or define('NIMO_VENDORS', NIMO_PATH.'/vendors'); //扩展文件夹

class Nimo
{
    private static $_classes = array();
    private static $_nimolang = array();
    private static $_app;
    
    /**
     * 获取版本号
     * @return string
     */
    public static function getVersion()
    {
        return '1.0';
    }
    
    /**
     * 创建应用程序
     * @param array 应用程序配置信息
     */
    public static function createApp($config=null)
    {
        return new CApplication($config);
    }
    
    public static function app()
    {
        return self::$_app;
    }
    
    public static function setApp($app)
    {
        if(self::$_app===null || $app===null) {
            self::$_app=$app;
        } else {
            throw new CException(Nimo::lang('app_is_created'));
        }
    }
    
    /**
     * 加载框架语言包
     * @param string 语句别名
     */
    public static function lang($alias)
    {
        self::$_nimolang = require_once(NIMO_PATH.'/lang/zh-cn.php');
        if(array_key_exists($alias, self::$_nimolang)) {
            return self::$_nimolang[$alias];
        } else {
            return $alias;
        }
    }
    
    /**
     * 导入类库
     * 
     */
    public static function import($libname)
    {
        return include_once(NIMO_PATH.'/lib/'.$libname.'.lib.php');
    }
    
    /**
     * 自动加载类文件
     * @param string 类名
     */
    public static function autoload($className)
    {
        if(isset(self::$_coreClasses[$className])) {
            require_once (NIMO_PATH.self::$_coreClasses[$className]);
        } elseif (file_exists(NIMO_PATH.'/lib/'.$className.".php")){
            require_once (NIMO_PATH.'/lib/'.$className.".php");
        } else {
            throw new CException(Nimo::lang('class_not_exists'));
            return false;
        }
    }
        
    /**
     * 版权信息
	 * @return string Powered by
	 */
    public static function powered()
    {
        return 'Powered by <a href="http://www.nimophp.com/" target="_blank">NimoPHP Framework</a>.';
    }
    
    /**
     * 核心类和接口集合
     * @var array 
     */
    public static $_coreClasses = array(
        'CConf'=>'/base/CConf.php',
        'CApplication' => '/base/CApplication.php',
        'CController' => '/base/CController.php',
        'CModel' => '/base/CModel.php',
        'CView' => '/base/CView.php',
        'CTemplate' => '/view/CTemplate.php',
        'CUrlManager' => '/base/CUrlManager.php',
        'CException' => '/base/CException.php',
        'CHttpException' => '/base/CHttpException.php',
        'CDbException' => '/base/CDbException.php',
        'CDb' => '/db/CDb.php',
        'Ischeme' => '/db/scheme/Ischeme.php',
    );
    
    /**
     * 外部扩展库
     * @var array
     */
    public static $_vendors = array(
        'PHPMailer' => 'PHPMailer/class.phpmailer.php',
        'Smarty' => 'Smarty/Smarty.class.php',
    );
    
    /**
     * dump 解决乱码问题
     * @param mixed $content
     */
    public static function dump($content)
    {
        header("Content-Type: text/html; charset=utf-8");
        var_dump($content);
    }
    
    /*
    public static function sreadfile($filename)
    {
        $content = '';
        if(function_exists('file_get_contents')) {
            @$content = file_get_contents($filename);
        } else {
            if(@$fp = fopen($filename, 'r')) {
                @$content = fread($fp, filesize($filename));
                @fclose($fp);
            }
        }
        return $content;
    }
    
    public static function swritefile($filename, $writetext, $openmod='w')
    {
        if(@$fp = fopen($filename, $openmod)) {
            flock($fp, 2);
            fwrite($fp, $writetext);
            fclose($fp);
            return true;
        } else {
            //runlog('error', "File: $filename write error.");
            return false;
        }
    }
    
    function saddslashes($string)
    {
        if(is_array($string)) {
            foreach($string as $key => $val) {
                $string[$key] = saddslashes($val);
            }
        } else {
            $string = addslashes($string);
        }
        return $string;
    }*/
}
spl_autoload_register(array('Nimo','autoload'));